package cn.kgc.controller;

import cn.kgc.pojo.PersonnelJ;
import cn.kgc.service.PersonnelService_jjb;

import cn.kgc.util.UploadUntilsJ;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: ${景军彪}
 * @date: 2022/2/18
 * @description:
 */
@Controller
public class Controller_jjb {

    @Resource
    private PersonnelService_jjb personnelService_jjb;

    @RequestMapping("/人事档案管理")
    public String renShiGuanLi(){
        return "人事档案管理";
    }

    //分页模糊查询
    @RequestMapping("/eyes")
    @ResponseBody
    public String eyes(@RequestParam(value = "personnelid",required = false) Integer personnelid,
                       @RequestParam("status") String status,
                       @RequestParam("fileStatus") String fileStatus,
                       @RequestParam("inputtingTime1") String inputtingTime1,
                       @RequestParam("inputtingTime2") String inputtingTime2,
                       @RequestParam("currentPage") Integer currentPage,
                       @RequestParam("pageSize") Integer pageSize){

        System.out.println("personnelid = " + personnelid);
        System.out.println("status = " + status);
        System.out.println("fileStatus = " + fileStatus);
        System.out.println("inputtingTime = " + inputtingTime1);
        System.out.println("inputtingTim2e = " + inputtingTime2);
        //核心点
        PageHelper.startPage(currentPage, pageSize);
        //得到分页查询的数据
        List<PersonnelJ> list = personnelService_jjb.selAll(personnelid,fileStatus,status,inputtingTime1,inputtingTime2);
        //非常合理化的去转换分页数据，携带总条数
        PageInfo<PersonnelJ> personnelPageInfo = new PageInfo<PersonnelJ>(list);
        System.out.println("总条数"+personnelPageInfo.getTotal());

        return JSONObject.toJSONStringWithDateFormat(personnelPageInfo,"yyyy-MM-dd");
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("/人事档案登记")
    public String renShiDengJi(){
//        List<PersonnelJ> list = personnelService_jjb.selAll(null, null, null, null, null);
//        System.out.println("list+添加"+list);
//        model.addAttribute("list",list);
        return "人事档案登记";
    }
    @RequestMapping("/save")
    @ResponseBody
    public String save(@ModelAttribute PersonnelJ personnelJ,
                        @RequestParam("file") MultipartFile file){
        //首先调用我们写的工具类

        String upload = UploadUntilsJ.upload(file);//获得了一个文件路径
        System.out.println(upload);
        personnelJ.setAttachment(upload);

        boolean b = personnelService_jjb.addPer(personnelJ);
        return JSON.toJSONString(b);
    }

    @RequestMapping("/人事档案明细/{personnelid}")
    public String renShiMingXi(Model model,
                               @PathVariable Integer personnelid){
        System.out.println("personnelid = " + personnelid);
        List<PersonnelJ> p = personnelService_jjb.getP(personnelid);
        model.addAttribute("per",p);
        return "人事档案明细";
    }

    @RequestMapping("/人事档案复核/{personnelid}")
    public String renShiFuHe(Model model,
                             @PathVariable Integer personnelid){
        System.out.println("personnelid = " + personnelid);
        List<PersonnelJ> p = personnelService_jjb.getP(personnelid);
        model.addAttribute("per",p);
        return "/人事档案复核";
    }
    @RequestMapping("/fuhe3")
    @ResponseBody
    public boolean fuhe3(@ModelAttribute PersonnelJ personnelJ){
        boolean b = personnelService_jjb.updFuHe(personnelJ);
        System.out.println("b复核 = " + b);
        return b;
    }

    @RequestMapping("/人事档案变更/{personnelid}")
    public String renShiBianGeng(Model model,
                                 @PathVariable Integer personnelid){
        System.out.println("personnelid = " + personnelid);
        List<PersonnelJ> p = personnelService_jjb.getP(personnelid);
        model.addAttribute("per",p);
        return "人事档案变更";
    }
    @RequestMapping("/bianGeng")
    @ResponseBody
    public boolean bianGeng(@ModelAttribute PersonnelJ personnelJ){
        boolean b = personnelService_jjb.updBianGeng(personnelJ);
        System.out.println("b变更 = " + b);
        return b;
    }
    @RequestMapping("/bianGeng2")
    @ResponseBody
    public boolean bianGeng2(@ModelAttribute PersonnelJ personnelJ){
        boolean b = personnelService_jjb.updBianGengTwo(personnelJ);
        System.out.println("b变更2 = " + b);
        return b;
    }


    @RequestMapping("/人事档案修改/{personnelid}")
    public String update(@ModelAttribute PersonnelJ personnelJ,
                          @PathVariable Integer personnelid,Model model){
        List<PersonnelJ> p = personnelService_jjb.getP(personnelid);
        model.addAttribute("per",p);
        boolean b = personnelService_jjb.updateP(personnelJ);
        System.out.println(b);
        return "人事档案修改";
    }
    @RequestMapping("/update2")
    @ResponseBody
    public boolean update2(@ModelAttribute PersonnelJ personnelJ){
        boolean b = personnelService_jjb.updateP(personnelJ);
        System.out.println("修改b = " + b);
        return b;
    }

    //删除
    @RequestMapping("/detail")
    @ResponseBody
    public boolean detail(@RequestParam Integer personnelid,
                          @RequestParam String shan){
        boolean b = personnelService_jjb.deleteP(personnelid,shan);
        System.out.println(b);
        return b;
    }
}
